<?php

class SetController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	
	public function init() {
		
		$this->commonAction();	
	
	}
	//用户首页
	public function indexAction() {
		try {

			$setting=$this->_dbAdapter->fetchAssoc('select variable,value,readme from setting');
			//$setting['actionlist']=explode(',',$setting['actionlist']);
			$this->view->settinglist=$setting;
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function opAction() {
		try {
			
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			$isok=0;
			foreach($p['old'] as $key => $value){
				$data=array('value'=>trim($value));
				$result=$this->_dbAdapter ->update ( 'setting',$data,'variable="'.$key.'"' );
				if($result)
					$isok=1;	
			}
			
			if(!empty($p['new']['key'])){
				//print_r($p['new']);
				$data=array('value'=>trim($p['new']['value']),'variable'=>trim($p['new']['key']),'readme'=>trim($p['new']['readme']));
				$result=$this->_dbAdapter ->insert ( 'setting',$data);
				if($result)
					$isok=1;	
			}
			
			if($isok)
				$this->feedback ( '更新成功', '注意', '/'.$this->controller, 'warning' );
			else	
				throw new Exception ( '更新失败或没有更改任何内容 ！' );
			
			//print_r($p);exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function photoAction(){
		try {
			$sql='SELECT value FROM `setting` where variable="photo_set"';
			$result=$this->_dbAdapter ->fetchOne ( $sql );
			$photo_set=Zend_Json::decode($result);
			$this->view->photo_set=$photo_set;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opeditAction(){
		try {
			if (! $this->isPost ()) {
				throw new Exception ( 'parameter_error' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
//Array ( [photo_set] => 语言,数学 ) 
			if(!$p['photo_set']){
				throw new Exception ( '内容不能为空' );
			}
			
			$data=array(
				'value'=>Zend_Json::encode($p['photo_set']),
			);
			//print_r($data);exit;
			$result = $this->_dbAdapter->update( 'setting', $data, 'variable="photo_set"');
			
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/photo' , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}


		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}


	
	public function scoreAction(){
		try {
			$sql="SELECT * FROM `setting`";
			$result=$this->_dbAdapter ->fetchAssoc ( $sql );
			//print_r($result);exit;
			$this->view->result=$result;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opeditscoreAction(){
		try {
			if (! $this->isPost ()) {
				throw new Exception ( 'parameter_error' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
//Array ( [score_photo] => 2 [score_term] => 3 [score_year] => 4 [score_recommend] => 5 [credit_expiretime] => 2013-11-06 [credit_shoplimit] => 12 [credit_max_vip] => 600 [credit_max_photo] => 400 [credit_max_publicize] => 5 [credit_shopnotice] => 5 ) 
			if(!$p['credit_shopnotice'] || !$p['credit_max_publicize'] || !$p['credit_max_photo'] || !$p['credit_max_vip'] || !$p['credit_shoplimit'] || !$p['credit_expiretime'] || !$p['score_photo'] || !$p['score_term'] || !$p['score_year'] || !$p['score_recommend'] ){
				throw new Exception ( '所填内容不能为空' );
			}

			$sql="update setting set value=".$p['credits_performance']." where variable='credits_performance'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".$p['score_photo']." where variable='credit_artice_photo'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".$p['score_term']." where variable='credit_vip_term'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".$p['score_year']." where variable='credit_vip_year'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".$p['score_recommend']." where variable='credit_teacher_publicize'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".$p['credit_shopnotice']." where variable='credit_shopnotice'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".$p['credit_max_publicize']." where variable='credit_max_publicize'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".$p['credit_max_photo']." where variable='credit_max_photo'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".$p['credit_max_vip']." where variable='credit_max_vip'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".$p['credit_shoplimit']." where variable='credit_shoplimit'";
			$result = $this->_dbAdapter->query ($sql);
			
			$sql="update setting set value=".strtotime($p['credit_expiretime'])." where variable='credit_expiretime'";
			$result = $this->_dbAdapter->query ($sql);
						
			if ($result) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/score' , 'tip', 3 );
			} else {
				throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
			}


		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	} 


	public function vipAction(){
		try {
			$sql="SELECT * FROM `vip_item`";
			$result=$this->_dbAdapter ->fetchAssoc ( $sql );
			
			foreach($result as $key=>$value){
				$result[$key]['cloudpath']=explode(',',$value['cloudpath']);
			}
			//print_r($result);exit;
			$this->view->result=$result;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function vipsaveAction() {
		try {
			
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			
			foreach($p['name'] as $key=>$value){
				if (!$value) {
						throw new Exception ( '名称不能为空！' );
				}
			}
			
			foreach($p['enname'] as $key=>$value){
				if (!$value) {
						throw new Exception ( '中文不能为空！' );
				}else{
					$p['enname'][$key]=trim($value);
				}
			}
			
			foreach($p['itemid'] as $key=>$value){
				$data=array(
					'name'=>$p['name'][$key],
					'enname'=>$p['enname'][$key],
					'addtime'=>time(),
				);
				
/*				if(isset($_FILES['pic_url']['tmp_name'][$key]) && !empty($_FILES['pic_url']['tmp_name'][$key])){
					  $type = '.jpg'; 
					  $dirname=PROJECT_ROOT .'/public/images/';	
					  if (!file_exists($dirname)) {
						  mkdir($dirname);
					  }  
					  //新文件名
					  $new_file_name_tmp = $p['enname'][$key];
					  
					  //移动文件 
					  $file_path = $dirname .'/'. $new_file_name_tmp;   
					  $new_file_name = $new_file_name_tmp.$type;//原图
					  $thumbname_big_tiny = $new_file_name_tmp.$type;//  小图 150*150
					  
					  if( $this->ThumbRatio($_FILES['pic_url']['tmp_name'][$key],$dirname.'/'.$thumbname_big_tiny,720,480,1)){//小图		
						  $res=Azure_Storage::defaultImg('images',$dirname.$thumbname_big_tiny,$thumbname_big_tiny);//上传到云服务器上面 1、文件夹的名字；2、文件的绝对路径；3、图片名字
					  } 
					  //print_r($thumbname_big_tiny);exit;
					  if($res){
						//删除本地图片
						unlink($dirname.$thumbname_big_tiny);
					  }else{
						throw new Exception ('失败');
					  }
					  $data['cloudpath']=$new_file_name_tmp;
				} 
*/				
				$res=$this->_dbAdapter->update('vip_item',$data,"itemid='".$value."'");
			}

			
			if($res)
				$this->feedback ( '更新成功', '注意', '/'.$this->controller.'/vip', 'warning' );
			else	
				throw new Exception ( '更新失败或没有更改任何内容 ！' );
			
			//print_r($p);exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function vipaddAction(){
		try {
			$sql="SELECT * FROM `vip_item`";
			$result=$this->_dbAdapter ->fetchAssoc ( $sql );
			//print_r($result);exit;
			$this->view->result=$result;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function vipaddsaveAction() {
		try {
			
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;
			if (!$p['name'] || !$p['enname']) {
					throw new Exception ( '名称和英文不能为空！' );
			}
			
			//print_r($_FILES['pic_url']);exit;
			$data=array(
				'name'=>$p['name'],
				'enname'=>$p['enname'],
				'addtime'=>time(),
			);
			$res=$this->_dbAdapter->insert('vip_item',$data);
			$itemid = $this->_dbAdapter->lastInsertId (); //获得刚插入id

			$data1=array(
				'itemid'=>$itemid,
				'viptype'=>0,
				'canlook'=>0,
				'canuse'=>0,
			);
			$res=$this->_dbAdapter->insert('vip_item_set',$data1);

			$data1=array(
				'itemid'=>$itemid,
				'viptype'=>1,
				'canlook'=>0,
				'canuse'=>0,
			);
			$res=$this->_dbAdapter->insert('vip_item_set',$data1);

			$data1=array(
				'itemid'=>$itemid,
				'viptype'=>2,
				'canlook'=>0,
				'canuse'=>0,
			);
			$res=$this->_dbAdapter->insert('vip_item_set',$data1);

			
			if(isset($_FILES['pic_url']['tmp_name']) && !empty($_FILES['pic_url']['tmp_name'])){
				  $type = '.jpg'; 
				  $dirname=PROJECT_ROOT .'/public/images/';	
				  if (!file_exists($dirname)) {
					  mkdir($dirname);
				  }  
				  //新文件名
				  $new_file_name_tmp = $p['enname'];
				  //移动文件 
				  $file_path = $dirname .'/'. $new_file_name_tmp;   
				  $new_file_name = $new_file_name_tmp.$type;//原图
				  $thumbname_big_tiny = $new_file_name_tmp.$type;//  小图 150*150
				  if( $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$dirname.'/'.$thumbname_big_tiny,720,480,1)){//小图		
					  $res=Azure_Storage::defaultImg('images',$dirname.$thumbname_big_tiny,$thumbname_big_tiny);//上传到云服务器上面 1、文件夹的名字；2、文件的绝对路径；3、图片名字
				  } 
				  if($res){
					//删除本地图片
					unlink($dirname.$thumbname_big_tiny);
				  }else{
					throw new Exception ('失败');
				  }
				  $data['cloudpath']=$new_file_name_tmp;
			} 
			$res=$this->_dbAdapter->update('vip_item',$data,"itemid='".$itemid."'");

			
			if($res)
				$this->feedback ( '添加成功', '注意', '/'.$this->controller.'/vip', 'warning' );
			else	
				throw new Exception ( '保存失败 ！' );
			
			//print_r($p);exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function vipdeleteAction(){
		try {
				
			$itemid = ( int ) $this->_request->getParam ( 'id' );
			if (! $itemid)
				throw new Exception ( '参数错误！' );
			
			$sql="select * from vip_item where itemid=".$itemid;
			$res=$this->_dbAdapter->fetchOne($sql);
			if($res){
				$sql="delete from vip_item where itemid=".$itemid;
				$result=$this->_dbAdapter->query($sql);
				
				$sql="delete from vip_item_set where viptype=0 and itemid=".$itemid;
				$result=$this->_dbAdapter->query($sql);
				$sql="delete from vip_item_set where viptype=1 and itemid=".$itemid;
				$result=$this->_dbAdapter->query($sql);
				$sql="delete from vip_item_set where viptype=2 and itemid=".$itemid;
				$result=$this->_dbAdapter->query($sql);
			}	
			if($result){
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/vip' , 'tip', 3 );
			}
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function vippictureAction(){
		try {
			$itemid = $this->_request->getParam ( 'itemid' );
			if(!$itemid)
				throw new Exception ( '参数错误' );

			$sql="SELECT * FROM `vip_item` where itemid=".$itemid;
			$result=$this->_dbAdapter ->fetchRow ( $sql );
			//print_r($result);exit;
			if($result['cloudpath']){
				$result['cloudpath']=explode(',',$result['cloudpath']);
			}
			//print_r($result);exit;
			$this->view->result=$result;
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
//vippicturesave
	public function vippicturesaveAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);echo '<br />';

			//print_r($_FILES['pic_url']);exit;
			$data=array();
			if(isset($_FILES['pic_url']['tmp_name']) && !empty($_FILES['pic_url']['tmp_name'])){
				  $type = '.jpg'; 
				  $dirname=PROJECT_ROOT .'/public/images/';	
				  if (!file_exists($dirname)) {
					  mkdir($dirname);
				  } 
				   
				  //新文件名
				  $arr=array();
				  $big=0;
				  if(isset($p['cloudpath'])){
					  foreach($p['cloudpath'] as $key=>$value){
						  $arr[$key]=explode('_',$value);
					  }
				  }
				  //print_r($arr);echo '<br />';
				  for($i=0;$i<count($arr)-1;$i++){
					  if(!isset($arr[$i][1])){
					  	  $arr[$i][1]=0;
					  }
					  for($j=$i+1; $j<count($arr);$j++){
						  if(!isset($arr[$j][1])){
							  $arr[$j][1]=0;
						  }
						  if($arr[$j][1]>$arr[$i][1]){
							  $big=$arr[$j][1];
							  $arr[$j][1]=$arr[$i][1];
							  $arr[$i][1]=$big;
						  }
					  }
				  }
				  //print_r($arr);exit;
				  if($arr[0][1]){
				  	  $name=$arr[0][1]+1; 
				  }else{
					  $name=1;
				  }
				  //print_r($name);exit;
				  $new_file_name_tmp = $p['enname'].'_'.$name;
				  //移动文件 
				  $file_path = $dirname .'/'. $new_file_name_tmp;   
				  $new_file_name = $new_file_name_tmp.$type;//原图
				  $thumbname_big_tiny = $new_file_name_tmp.$type;//  小图 150*150
				  if( $this->ThumbRatio($_FILES['pic_url']['tmp_name'],$dirname.'/'.$thumbname_big_tiny,720,480,1)){//小图		
					  $res=Azure_Storage::defaultImg('images',$dirname.$thumbname_big_tiny,$thumbname_big_tiny);//上传到云服务器上面 1、文件夹的名字；2、文件的绝对路径；3、图片名字
				  }
				  //print_r($res);exit; 
				  if($res){
					//删除本地图片
					unlink($dirname.$thumbname_big_tiny);
				  }else{
					throw new Exception ('失败');
				  }
				  $data['cloudpath']=$new_file_name_tmp;
			} 
			//print_r($data);exit;
			
			if(isset($p['picture']) && isset($p['cloudpath'])){
				foreach($p['cloudpath'] as $key=>$value){
					foreach($p['picture'] as $k=>$v){
						if($value==$v){
							unset($p['cloudpath'][$key]);
							//print_r($p);exit;
						}
					}
				}
				//print_r($p['cloudpath']);exit;
			}
			$p['cloudpath']=implode(',',$p['cloudpath']);
			
			if($data['cloudpath'] && $p['cloudpath']){
				$data['cloudpath'].=','.$p['cloudpath'];
			}elseif(!$data['cloudpath'] && $p['cloudpath']){
				$data['cloudpath']=$p['cloudpath'];
			}elseif($data['cloudpath'] && !$p['cloudpath']){
				$data['cloudpath']=$data['cloudpath'];
			}else{
				$data['cloudpath']='';
			}
			if($data){
				$res=$this->_dbAdapter->update('vip_item',$data,"itemid='".$p['itemid']."'");
			}
			if($res)
				$this->feedback ( '成功', '注意', '/'.$this->controller.'/vippicture/itemid/'.$p['itemid'], 'warning' );
			else	
				throw new Exception ( '失败 ！' );
			
			//print_r($p);exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}


	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

